﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.Reflection;
using System.ComponentModel;

namespace HandRise.MBHOP.Controls
{
    public class CardControl : Panel
    {

        private Color titleForeColor = Color.Red;
        /// <summary>
        /// 标题的前景色
        /// </summary>
        public Color TitleForeColor
        {
            get
            {
                return titleForeColor;
            }
            set
            {
                titleForeColor = value;
            }
        }

        private Color contentForeColor = Color.Black;
        /// <summary>
        /// 内容的前景色
        /// </summary>
        public Color ContentForeColor
        {
            get
            {
                return contentForeColor;
            }
            set
            {
                contentForeColor = value;
            }
        }

        //private int titleFontSize = 12;
        ///// <summary>
        ///// 标题的字号

        ///// </summary>
        //public int TitleFontSize
        //{
        //    get
        //    {
        //        return titleFontSize;
        //    }
        //    //set
        //    //{
        //    //    titleFontSize = value;
        //    //}
        //}

        private int fontSize = 12;
        /// <summary>
        /// 内容的字号

        /// </summary>
        [DefaultValue(12)]
        public int FontSize
        {
            get
            {
                return fontSize;
            }
            set
            {
                fontSize = value;
                //titleFontSize = fontSize;
            }
        }

        //总页数

        //int pageCount = 0;
        /// <summary>
        /// 总页数

        /// </summary>
        public int PageCount
        {
            get
            {
                return textPage.Count ;
            }
        }

        //当前页码
        int currPageIndex = 1;
        public int CurrPageIndex
        {
            get
            {
                return currPageIndex;
            }
        }

        private object dataSource = null;
        /// <summary>
        /// 数据源

        /// </summary>
        public object DataSource
        {
            get
            {
                return dataSource;
            }
            set
            {
                dataSource = value;
                //DataBind();
            }
        }
        /// <summary>
        /// 要显示的属性列表

        /// </summary>
        private List<TextMemberPro> textMemberList = new List<TextMemberPro>();

        /// <summary>
        /// 测量字符串图面

        /// </summary>
        private Bitmap bit = null;
        Graphics g = null;

        /// <summary>
        /// 所有的文字页

        /// </summary>
        private List<Panel> textPage = new List<Panel>();

        private int currPageHeight = 0;

        private int currMemberIndex = 0;

        private bool allComplete = false;

        public int exeTime = 0;

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void DataBind()
        {
            int startTime = System.Environment.TickCount;
            textPage.Clear();
            currPageHeight = 0;
            currMemberIndex = 0;
            allComplete = false;
            currPageIndex = 1;
            foreach (TextMemberPro textMemberPro in textMemberList)
            {
                textMemberPro.ReSet();
            }

            bit = new Bitmap(this.Width, this.Height);
            g = Graphics.FromImage(bit);
            if (dataSource != null)
            {
                Panel newPanel = new Panel();
                newPanel.Size = this.Size;
                newPanel.Location = new Point(0, 0);
                newPanel.BackColor = this.BackColor;
                textPage.Add(newPanel);

                ShowTextMember(TurnType.Page);
            }
            this.Controls.Clear();
            if (textPage.Count > 0)
            {
                this.Controls.Add(textPage[0]);
                //pageCount = textPage.Count;
            }
            //g.Dispose();
            //bit.Dispose();
            exeTime = System.Environment.TickCount - startTime;
        }

        /// <summary>
        /// 显示属性

        /// </summary>
        /// <param name="textMemberPro"></param>
        private void ShowTextMember(TurnType turnType)
        {
            if (fontSize <= 0)
            {
                allComplete = true;
                return;
            }
            bool turnNextPro = false;

            for (int i = 0; i < textMemberList.Count; i++)
            {
                //属性写完则写下一个

                if (textMemberList[i].IsComplete)
                    continue;
                //PropertyInfo proContent = dataSource.GetType().GetProperty(textMemberList[i].TextMember);
                PropertyInfo proContent = GetPropertyInfo(textMemberList[i].TextMember);
                if (textMemberList[i].CurrIndexInTitle && textMemberList[i].MemberTitle != "" && proContent != null)
                {
                    if (ShowString(textMemberList[i], textMemberList[i].MemberTitle, new Font("雅黑", FontSize, FontStyle.Regular), TitleForeColor, true) && turnType == TurnType.Page)
                    {
                        textMemberList[i].MemberPageIndex = textPage.Count - 1;
                        turnNextPro = true;
                        break;
                    }
                    else
                    {

                        textMemberList[i].MemberPageIndex = textPage.Count - 1;
                        turnNextPro = true;
                    }

                }

                //如果写完标题没有分页则当前索引放入内容中
                textMemberList[i].CurrIndexInTitle = false;

                if (proContent != null)
                {
                    //object Obj = proContent.GetValue(dataSource, null);
                    object Obj = GetPropertyValue(textMemberList[i].TextMember);
                    if (Obj != null)
                    {
                        if (ShowString(textMemberList[i], textMemberList[i].MemberTitle + "   " + Obj.ToString(), new Font("雅黑", FontSize, FontStyle.Regular), ContentForeColor, false))
                        {
                            if (turnType == TurnType.Page)
                                break;
                            else if (turnNextPro)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        if (ShowString(textMemberList[i], textMemberList[i].MemberTitle + "   "  , new Font("雅黑", FontSize, FontStyle.Regular), ContentForeColor, false))
                        {
                            if (turnType == TurnType.Page)
                                break;
                            else if (turnNextPro)
                            {
                                break;
                            }
                        }
                    }
                }
                //当前属性写完

                textMemberList[i].IsComplete = true;
                if (i == textMemberList.Count - 1)
                    allComplete = true;
            }
        }

        private PropertyInfo GetPropertyInfo(string memberName)
        {
            try
            {
                PropertyInfo proContent = null;
                string[] propertys = memberName.Split('.');
                Type lastType = dataSource.GetType();
                foreach (string property in propertys)
                {
                    proContent = lastType.GetProperty(property);
                    lastType = proContent.PropertyType;
                }
                return proContent;
            }
            catch (Exception)
            {

                return null;
            }

        }

        private object GetPropertyValue(string memberName)
        {
            try
            {
                PropertyInfo proContent = null;
                string[] propertys = memberName.Split('.');
                Type lastType = dataSource.GetType();
                object lastObj = dataSource;
                foreach (string property in propertys)
                {
                    proContent = lastType.GetProperty(property);
                    lastType = proContent.PropertyType;
                    lastObj = proContent.GetValue(lastObj, null);
                }
                return lastObj;
            }
            catch (Exception)
            {

                return null;
            }

        }

        /// <summary>
        /// 增加文字显示
        /// </summary>
        /// <param name="textMemberPro"></param>
        /// <param name="text"></param>
        /// <param name="font"></param>
        /// <param name="foreColor"></param>
        /// <param name="isTitle"></param>
        /// <returns>是否要增加新页</returns>
        private bool ShowString(TextMemberPro textMemberPro, string text, Font font, Color foreColor, bool isTitle)
        {
            StringBuilder sb = new StringBuilder(text, textMemberPro.CurrIndex, text.Length - textMemberPro.CurrIndex, 1000);
            SizeF sizeF = g.MeasureString(sb.ToString(), font);
            int trunPageIndex = 0;
            int fontHeight = Convert.ToInt32(sizeF.Height);
            if (sizeF.Width > this.Width - font.Size)
            {
                for (int i = 0; i < sb.Length; i++)
                {
                    sizeF = g.MeasureString(sb.ToString(trunPageIndex, i - trunPageIndex), font);

                    if (sizeF.Width > this.Width - font.Size)
                    {
                        //多行情况，需要增加新页时，先添加文字显示，再增加新页
                        //if (g.MeasureString(sb.ToString(trunPageIndex, i - trunPageIndex), font).Height + currPageHeight > this.Height - 15)
                        if (sizeF.Height + currPageHeight > this.Height - fontHeight)
                        {
                            AddLabel(font, foreColor, sizeF, sb.ToString(trunPageIndex, i - trunPageIndex - 1), isTitle);
                            //if (!isTitle)
                            //{
                            AddPage();
                            //}
                            textMemberPro.CurrIndex = i - trunPageIndex - 1 - sb.Length + text.Length;
                            return true;
                        }
                        else
                        {
                            sb.Insert(i - 1, "\r\n");
                        }
                    }
                    if (i == sb.Length - 1)
                    {
                        AddLabel(font, foreColor, sizeF, sb.ToString(trunPageIndex, i - trunPageIndex + 1), isTitle);
                        if (!isTitle)
                            currPageHeight = currPageHeight + (int)sizeF.Height + fontHeight;
                    }
                }
            }
            else
            {
                //单行情况先增加新页再增加显示
                if (sizeF.Height + currPageHeight > this.Height - fontHeight)
                {
                    AddPage();
                }
                AddLabel(font, foreColor, sizeF, sb.ToString(), isTitle);
                if (!isTitle)
                    if (sizeF.Height + currPageHeight > this.Height - fontHeight)
                    {
                        currPageHeight = (int)sizeF.Height + 15;
                        return true;
                    }
                    else
                    {
                        currPageHeight = currPageHeight + (int)sizeF.Height + fontHeight;
                        return false;
                    }
            }
            return false;
        }

        private void AddPage()
        {
            Panel newPanel = new Panel();
            newPanel.Size = this.Size;
            newPanel.Location = new Point(0, 0);
            newPanel.BackColor = this.BackColor;
            textPage.Add(newPanel);
            currPageHeight = 0;
        }

        private void AddLabel(Font font, Color foreColor, SizeF size, string text, bool isTitle)
        {
            Label lab = new Label();
            lab.Font = font;
            lab.ForeColor = foreColor;
            lab.Location = new Point(Convert.ToInt32(font.Size) / 2, currPageHeight);
            if (isTitle)
                lab.Size = new Size((int)size.Width + Convert.ToInt32(font.Size), (int)size.Height);
            else
                lab.Size = new Size((int)size.Width + Convert.ToInt32(font.Size), (int)size.Height);

            lab.Text = text;
            textPage[textPage.Count - 1].Controls.Add(lab);
            textPage[textPage.Count - 1].Controls.SetChildIndex(lab, textPage[textPage.Count - 1].Controls.Count - 1);
        }

        public bool NextPage()
        {
            if (dataSource == null || textMemberList.Count == 0)
                return true;
            int startTime = System.Environment.TickCount;
            //如果当前是最后一页

            if (allComplete && currPageIndex == textPage.Count)
            {
                exeTime = System.Environment.TickCount - startTime;
                return true;
            }
            else
            {
                if (!allComplete && currPageIndex == textPage.Count - 1)
                    ShowTextMember(TurnType.Page);
                //页码加一
                currPageIndex = currPageIndex + 1;
                //清除页

                this.Controls.Clear();
                //显示页

                if (currPageIndex - 1 < textPage.Count)
                    this.Controls.Add(textPage[currPageIndex - 1]);
                else
                    return true;

                //纪录当前的属性索引

                for (int i = textMemberList.Count - 1; i >= 0; i--)
                {
                    if (currPageIndex - 1 == textMemberList[i].MemberPageIndex)
                    {
                        currMemberIndex = i;
                        break;
                    }
                }
                exeTime = System.Environment.TickCount - startTime;
                if (allComplete && currPageIndex == textPage.Count)
                    return true;
                else
                    return false;
            }
        }

        public bool PreviousPage()
        {
            if (dataSource == null || textMemberList.Count == 0)
                return true;
            int startTime = System.Environment.TickCount;
            if (currPageIndex == 1)
            {
                exeTime = System.Environment.TickCount - startTime;
                return true;
            }
            else
            {
                currPageIndex = currPageIndex - 1;
                this.Controls.Clear();
                if (currPageIndex - 1 < textPage.Count)
                    this.Controls.Add(textPage[currPageIndex - 1]);
                else
                    return false;

                for (int i = textMemberList.Count - 1; i >= 0; i--)
                {
                    if (currPageIndex - 1 == textMemberList[i].MemberPageIndex)
                    {
                        currMemberIndex = i;
                        break;
                    }
                }
                exeTime = System.Environment.TickCount - startTime;
                if (currPageIndex == 1)
                    return true;
                else
                    return false;
            }
        }

        public bool NextProperty()
        {
            if (dataSource == null || textMemberList.Count == 0)
                return true;
            int startTime = System.Environment.TickCount;
            if (currMemberIndex == textMemberList.Count - 1 || currPageIndex == textPage.Count)
            {
                exeTime = System.Environment.TickCount - startTime;
                return true;
            }
            //if (!allComplete)
            //    ShowTextMember(TurnType.Property);
            for (int i = currMemberIndex; i < textMemberList.Count; i++)
            {
                if (textMemberList[i].MemberPageIndex != textMemberList[currMemberIndex].MemberPageIndex)
                {
                    if (!textMemberList[i].IsComplete && textMemberList[i].CurrIndexInTitle)
                        ShowTextMember(TurnType.Property);
                    currMemberIndex = i;
                    currPageIndex = textMemberList[i].MemberPageIndex + 1;
                    this.Controls.Clear();
                    this.Controls.Add(textPage[currPageIndex - 1]);
                    break;
                }
            }
            exeTime = System.Environment.TickCount - startTime;
            if (currMemberIndex == textMemberList.Count - 1 || currPageIndex == textPage.Count)
                return true;
            else
                return false;
        }

        public bool PreviousProperty()
        {
            if (dataSource == null || textMemberList.Count == 0)
                return true;
            int startTime = System.Environment.TickCount;
            if (currMemberIndex == 0 || currPageIndex == 1)
            {
                exeTime = System.Environment.TickCount - startTime;
                return true;
            }
            for (int i = currMemberIndex; i >= 0; i--)
            {
                if (textMemberList[i].MemberPageIndex != textMemberList[currMemberIndex].MemberPageIndex)
                {
                    currMemberIndex = i;
                    currPageIndex = textMemberList[i].MemberPageIndex + 1;
                    this.Controls.Clear();
                    this.Controls.Add(textPage[currPageIndex - 1]);
                    break;
                }
            }
            exeTime = System.Environment.TickCount - startTime;
            if (currMemberIndex == 0 || currPageIndex == 1)
                return true;
            else
                return false;

        }


        /// <summary>
        /// 添加显示的属性

        /// </summary>
        /// <param name="textMember"></param>
        /// <param name="memberTitle"></param>
        public void AddMember(string textMember, string memberTitle)
        {
            TextMemberPro textMemberObj = new TextMemberPro(textMember, memberTitle);
            textMemberList.Add(textMemberObj);

        }

        /// <summary>
        /// 清除所有属性
        /// </summary>
        public void ClearAllMember()
        {
            textMemberList.Clear();
        }

        internal class TextMemberPro
        {
            /// <summary>
            /// 构造函数

            /// </summary>
            /// <param name="textMember"></param>
            /// <param name="memberTitle"></param>
            public TextMemberPro(string textMember, string memberTitle)
            {
                this.TextMember = textMember;
                this.MemberTitle = memberTitle;
            }

            /// <summary>
            /// 值复位

            /// </summary>
            public void ReSet()
            {
                currIndexInTitle = true;
                currIndex = 0;
                isComplete = false;
                memberPageIndex = -1;
            }

            private string textMember = "";
            /// <summary>
            /// 要显示的属性

            /// </summary>
            public string TextMember
            {
                get
                {
                    return textMember;
                }
                set
                {
                    textMember = value;
                }
            }

            private string memberTitle = "";
            /// <summary>
            /// 属性的中文名称
            /// </summary>
            public string MemberTitle
            {
                get
                {
                    return memberTitle;
                }
                set
                {
                    memberTitle = value;
                }
            }

            public bool currIndexInTitle = true;
            /// <summary>
            /// 当前的索引在标题
            /// </summary>
            public bool CurrIndexInTitle
            {
                get
                {
                    return currIndexInTitle;
                }
                set
                {
                    currIndexInTitle = value;
                }
            }

            public int currIndex = 0;
            /// <summary>
            /// 当前的索引

            /// </summary>
            public int CurrIndex
            {
                get
                {
                    return currIndex;
                }
                set
                {
                    currIndex = value;
                }
            }

            public bool isComplete = false;
            /// <summary>
            /// 是否显示完毕
            /// </summary>
            public bool IsComplete
            {
                get
                {
                    return isComplete;
                }
                set
                {
                    isComplete = value;
                }
            }

            private int memberPageIndex = -1;
            /// <summary>
            /// 属性所在页
            /// </summary>
            public int MemberPageIndex
            {
                get
                {
                    return memberPageIndex;
                }
                set
                {
                    memberPageIndex = value;
                }
            }
        }
    }
}


